package com.polymer.app;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.polymer.app.entity.BussinessDO;
import com.polymer.app.service.BussinessService;
import com.polymer.app.service.TestService;
import com.polymer.app.utils.LoggerUtil;


@RestController
public class TestServiceController {
    private final static Logger logger =Logger.getLogger(TestServiceController.class);
    
    @Resource(name="testServiceImpl")
    private TestService testService;
    
    @Autowired
    private BussinessService bussinessService;
    
    
    @ResponseBody
    @RequestMapping(value="getstr")
    public String getStr(){
        String str = testService.getStr("123");
        return str;
    }
    
    
    @ResponseBody
    @RequestMapping(value="getbus")
    public List<BussinessDO> getBus(){
        LoggerUtil.info(logger, "getbus请求");
        List<BussinessDO> bbs=new ArrayList<BussinessDO>();
        BussinessDO getbus = bussinessService.getbus("fanxianlianxifangshi");
        bbs.add(getbus);
        getbus = bussinessService.getbus("1575007728649");
        bbs.add(getbus);
        getbus = bussinessService.getbus("1575007790093");
        bbs.add(getbus);
        LoggerUtil.info(logger, "getbus出参:{0}", bbs);
        return bbs;
    }
    
    @ResponseBody
    @RequestMapping(value="getbus1")
    public BussinessDO getBus1(){
        LoggerUtil.info(logger, "getbus请求");
        BussinessDO getbus = bussinessService.getbus("fanxianlianxifangshi");
        LoggerUtil.info(logger, "getbus出参:{0}", getbus);
        return getbus;
    }
    
    @ResponseBody
    @RequestMapping(value="getbus2")
    public BussinessDO getBus2(){
        LoggerUtil.info(logger, "getbus请求");
        BussinessDO getbus =  bussinessService.getbus("1575007728649");
        LoggerUtil.info(logger, "getbus出参:{0}", getbus);
        return getbus;
    }
    
    @ResponseBody
    @RequestMapping(value="getbus3")
    public BussinessDO getBus3(){
        LoggerUtil.info(logger, "getbus请求");
        BussinessDO getbus =  bussinessService.getbus("1575007790093");
        LoggerUtil.info(logger, "getbus出参:{0}", getbus);
        return getbus;
    }
    
    @ResponseBody
    @RequestMapping(value="getbusNxml")
    public BussinessDO getBusNoXML(){
        BussinessDO getbus = bussinessService.getbusNoXML("fanxianlianxifangshi");
        return getbus;
    }
    
    @RequestMapping(value = "/api/qrcode", produces = { "application/json;charset=UTF-8" })
    @ResponseBody
    public String getQrCode(HttpServletRequest request) {
        Map<String, String> requestData = getRequestParams(request);
        return JSON.toJSONString(requestData);
    }
    
    public static Map<String, String> getRequestParams(HttpServletRequest request) {
        Map<String, String> map = new HashMap<String, String>();
        @SuppressWarnings("unchecked")
        Enumeration<String> er = request.getParameterNames();
        while (er.hasMoreElements()) {
            String name = (String) er.nextElement();
            String value = request.getParameter(name);
            LoggerUtil.info(logger, "name：{0},value:{1}", name, value);
            map.put(name, value);
        }
        return map;
    }

    private static String decord(String str) {
        String result = null;
        if (null == str) {
            return "";
        }
        try {
            str = str.replaceAll("\\+", "%2B");
            result = java.net.URLDecoder.decode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }
}
